﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class DAO_Khoa
    {

        public static List<DTO_Khoa> SelectKhoa()
        {
            List<DTO_Khoa> listKhoa = new List<DTO_Khoa>();
            try
            {
                string s = "Proc_SelectKhoa";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_Khoa khoa = new DTO_Khoa();
                    khoa.MaKhoa = dt.Rows[i]["MaKhoa"].ToString();
                    khoa.TenKhoa = dt.Rows[i]["TenKhoa"].ToString();
                    listKhoa.Add(khoa);
                }
                ketnoi.Cmb().Close();
                return listKhoa;
            }
            catch (Exception ex)
            {
                return listKhoa;
            }
        }
        public static bool deleteKhoa(string makhoa)
        {
            List<DTO_Khoa> listKhoa = new List<DTO_Khoa>();
            // lay danh sach Khoa hoc theo Ma Khoa
            try
            {
                string s = "Proc_DeleteKhoa";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaKhoa", SqlDbType.NVarChar).Value = makhoa;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable sv = new DataTable();
                da.Fill(sv);
                for (int i = 0; i < sv.Rows.Count; i++)
                {
                    DTO_Khoa khoa = new DTO_Khoa();
                    khoa.MaKhoa = sv.Rows[i]["MaKhoa"].ToString();
                    listKhoa.Add(khoa);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool updateKhoa(DTO_Khoa khoa, string makhoa_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateKhoa";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaKhoa", khoa.MaKhoa);
                cmd1.Parameters.AddWithValue("@TenKhoa", khoa.TenKhoa);
                cmd1.Parameters.AddWithValue("@MaKhoa_Cu", makhoa_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool insertKhoa(DTO_Khoa khoa)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertKhoa";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaKhoa", khoa.MaKhoa);
                cmd1.Parameters.AddWithValue("@TenKhoa", khoa.TenKhoa);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
